<?php

if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {

    require_once './includes/db.php';

    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    $mysqli->set_charset("utf8");

    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL with MySQLI: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }


    
    
    if (!isset($_POST['valeur'])) {

        $sql = "update and_config set valeur='désactivé' where attribut='inscription'";

        $mysqli->query($sql);

        
        
        if ($mysqli->affected_rows > 0) {

            $msg_inscription = " Inscription <font color=\"red\"><b>désactivée</b></font> ";


            $mysqli->close();

            echo $msg_inscription;
            exit();
        } else {
            echo " <font color='red'><b>Erreur de modification de l'état de l'inscription</b></font>";
            exit();
        }
    } else {

        if (isset($_POST['date'])) {


            $dat = $_POST['date'];
        } else {
            $sql0 = "select valeur, date from and_config where attribut='inscription'";
            $result0 = $mysqli->query($sql0);

            $row0 = $result0->fetch_assoc();

            $dat = $row0['date'];
        }


        $sql1 = "update and_config set valeur = '".$_POST['valeur']."', date='" . $dat . "' where attribut='inscription'";
        
        $mysqli->query($sql1);

        $msg_inscription = " Inscription <font color=\"green\"><b>activée</b></font>  jusqu'au : <input type=\"text\" id=\"date\" name=\"date\" value=\"$dat\" /><script>$('#date').not('.hasDatePicker').datepicker({dateFormat : \"yy-mm-dd\", showButtonPanel: true, changeMonth: true, changeYear: true });</script>";
             
        echo $msg_inscription;
        
        exit();
    }
}
exit();
?>
